package com.example.cnotebook;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.cnotebook.adapter.HomeListViewAdapter;
import com.example.cnotebook.pojo.CategoryInfo;
import com.example.cnotebook.pojo.NoteInfo;
import com.example.cnotebook.util.OKHttpUtil;
import com.example.cnotebook.util.UserConstant;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 分类里面的笔记
 */
public class NotesActivity extends BaseActivity implements OKHttpUtil.CallBack, AdapterView.OnItemClickListener, View.OnClickListener {
    private ImageView ivBack;
    private ListView listview;
    private TextView tvEmpty;
    private TextView tvTitle;

    private int categoryId;
    private String categoryName;
    private HomeListViewAdapter adapter;
    private ArrayList<NoteInfo> noteInfos = new ArrayList<>();
    private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
    private ArrayList<NoteInfo> categoryNoteInfos = new ArrayList<>();

    private Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes);

        categoryId = getIntent().getIntExtra("categoryId", -1);
        categoryName = getIntent().getStringExtra("categoryName");

        ivBack = findViewById(R.id.iv_back);
        listview = findViewById(R.id.listview);
        tvEmpty = findViewById(R.id.tv_empty);
        tvTitle = findViewById(R.id.tv_title);

        tvTitle.setText(categoryName);

        adapter = new HomeListViewAdapter(this, noteInfos);
        listview.setAdapter(adapter);
        listview.setEmptyView(tvEmpty);

        listview.setOnItemClickListener(this);
        ivBack.setOnClickListener(this);
        getDate();
    }

    private void getDate() {
        categoryNoteInfos.clear();
        OKHttpUtil.get("/note/findByCategoryId/" + categoryId, this, this);
    }

    @Override
    public void onSuccess(JSONObject response) throws JSONException {
        if (response.getInt("code") == 1000) {
            JSONArray data = response.getJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                JSONObject jsonObject = data.getJSONObject(i);
                NoteInfo noteInfo = gson.fromJson(jsonObject.toString(), NoteInfo.class);
                categoryNoteInfos.add(noteInfo);
            }
            initData();
        }
    }

    private void initData() {
        noteInfos.clear();
        ArrayList<CategoryInfo> categoryInfos = UserConstant.categoryInfos;
        for (int i = 0; i < categoryNoteInfos.size(); i++) {
            NoteInfo noteInfo = categoryNoteInfos.get(i);
            noteInfo.setCreateDateTime(noteInfo.getCreateDate());
            if (noteInfo.getCategoryId() == -1) noteInfo.setCategoryName("未分类");
            noteInfo.setCreateDate(null);
            for (CategoryInfo categoryInfo : categoryInfos) {
                if (categoryInfo.getId().equals(noteInfo.getCategoryId())) {
                    noteInfo.setCategoryName(categoryInfo.getName());
                }
            }
            this.noteInfos.add(noteInfo);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(int errorNo, String strMsg) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, EditNoteActivity.class);
        intent.putExtra("nodeId", noteInfos.get(position).getId());
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (UserConstant.homeIsSaveData)
            getDate();
    }
}
